package com.lc.luzhengtang;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lc.luzhengtang.activity.LoginActivity;
import com.lc.luzhengtang.activity.ShopDetailActivity;
import com.lc.luzhengtang.fragment.ClassificationFragment;
import com.lc.luzhengtang.fragment.ISlideCallback;
import com.lc.luzhengtang.fragment.IndexFragment;
import com.lc.luzhengtang.fragment.MineFragment;
import com.lc.luzhengtang.fragment.ShoppingFragment2;
import com.lc.luzhengtang.util.LovelyToast;
import com.zcx.helper.activity.AppV4Activity;
import com.zcx.helper.fragment.navigation.NavigationManager;
import com.zcx.helper.fragment.navigation.NavigationManagerFactory;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

import static com.lc.luzhengtang.R.id.ll_main_tab_01;


public class MainActivity extends AppV4Activity implements View.OnClickListener, ISlideCallback {


    IndexFragment indexFragment;
    ClassificationFragment classFicationFragment;
    ShoppingFragment2 shoppingFragment;
    MineFragment mineFragment;
    @BindView(R.id.content)
    FrameLayout content;
    @BindView(R.id.iv_main_tab_01)
    ImageView ivMainTab01;
    @BindView(R.id.tv_main_tab_01)
    TextView tvMainTab01;
    @BindView(ll_main_tab_01)
    LinearLayout llMainTab01;
    @BindView(R.id.iv_main_tab_05)
    ImageView ivMainTab05;
    @BindView(R.id.tv_main_tab_05)
    TextView tvMainTab05;
    @BindView(R.id.ll_main_tab_05)
    LinearLayout llMainTab05;
    @BindView(R.id.iv_main_tab_02)
    ImageView ivMainTab02;
    @BindView(R.id.tv_main_tab_02)
    TextView tvMainTab02;
    @BindView(R.id.ll_main_tab_02)
    LinearLayout llMainTab02;
    @BindView(R.id.iv_main_tab_03)
    ImageView ivMainTab03;
    @BindView(R.id.tv_main_tab_03)
    TextView tvMainTab03;
    @BindView(R.id.ll_main_tab_03)
    LinearLayout llMainTab03;
    @BindView(R.id.iv_main_tab_04)
    ImageView ivMainTab04;
    @BindView(R.id.tv_main_tab_04)
    TextView tvMainTab04;
    @BindView(R.id.ll_main_tab_04)
    LinearLayout llMainTab04;
    @BindView(R.id.view_index_first)
    View viewIndexFirst;
    @BindView(R.id.view_index_classfical)
    View viewIndexClassfical;
    @BindView(R.id.view_index_third)
    View viewIndexThird;
    @BindView(R.id.view_index_fourth)
    View viewIndexFourth;
    private FragmentTransaction ft;

    //fragment的集合
    private NavigationManager<Fragment> navigationManager;


    private String flag = "";

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("onRestart", "uid = " + BaseApplication.basePreferences.readUid());
        if (TextUtils.isEmpty(BaseApplication.basePreferences.readUid())) {
            List<Fragment> fragments = getSupportFragmentManager().getFragments();
            for (Fragment fragment : fragments) {
                if (fragment instanceof MineFragment) {
                    ((MineFragment) fragment).refresh();
                }
            }
//            if (mineFragment != null) {
//                mineFragment.refresh();
//            }
        }
    }

    public BroadcastReceiver myReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
//         onClick(shoppingCar);

            // onViewClicked(ivMainTab02);

            onClick(llMainTab02);
//            setSelect(2);
        }
    };

    public BroadcastReceiver myReceiver2 = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                onClick(llMainTab01);
            } catch (Exception e) {

            }

//            setSelect(2);
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        EventBus.getDefault().register(this);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
       /* flag=getIntent().getStringExtra("flag");
        if (flag.equals("")){


        }else {
            setSelect(2);

        }*/
        LocalBroadcastManager.getInstance(this).registerReceiver(myReceiver, new IntentFilter(ShopDetailActivity.TURN_TO_CART));
        LocalBroadcastManager.getInstance(this).registerReceiver(myReceiver2, new IntentFilter(LoginActivity.TURNTOINDEX));
//        setSelect(0);

        navigationManager = NavigationManagerFactory.createV4(this, R.id.content);
        // noinspection unchecked
        navigationManager.addFragment(IndexFragment.class, ClassificationFragment.class, ShoppingFragment2.class, MineFragment.class);
//        ll_main_tab_01
        llMainTab01.setOnClickListener(this);
        llMainTab02.setOnClickListener(this);
        llMainTab05.setOnClickListener(this);
        llMainTab04.setOnClickListener(this);

        onClick(llMainTab01);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(myReceiver);
        LocalBroadcastManager.getInstance(this).unregisterReceiver(myReceiver2);
    }

/*
    @OnClick({R.id.content, R.id.iv_main_tab_01, R.id.tv_main_tab_01, ll_main_tab_01, R.id.iv_main_tab_05, R.id.tv_main_tab_05, R.id.ll_main_tab_05, R.id.iv_main_tab_02, R.id.tv_main_tab_02, R.id.ll_main_tab_02, R.id.iv_main_tab_03, R.id.tv_main_tab_03, R.id.ll_main_tab_03, R.id.iv_main_tab_04, R.id.tv_main_tab_04, R.id.ll_main_tab_04})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.content:
                break;
            case R.id.iv_main_tab_01:
                setSelect(0);
                break;
            case R.id.tv_main_tab_01:
                setSelect(0);
                break;
            case ll_main_tab_01:

                setSelect(0);

                break;
            case R.id.iv_main_tab_05:
                setSelect(1);
                break;
            case R.id.tv_main_tab_05:
                setSelect(1);
                break;
            case R.id.ll_main_tab_05:
                setSelect(1);
                break;
            case R.id.iv_main_tab_02:
                setSelect(2);
                break;
            case R.id.tv_main_tab_02:
                setSelect(2);
                break;
            case R.id.ll_main_tab_02:
                setSelect(2);
                break;
            case R.id.iv_main_tab_03:
                break;
            case R.id.tv_main_tab_03:
                break;
            case R.id.ll_main_tab_03:
                break;
            case R.id.iv_main_tab_04:
                setSelect(3);
                break;
            case R.id.tv_main_tab_04:
                setSelect(3);
                break;
            case R.id.ll_main_tab_04:
                setSelect(3);
                break;
        }
    }
*/


/*    private void setSelect(int i) {
        FragmentManager fm = getSupportFragmentManager();
        ft = fm.beginTransaction();
        reSetTab();
        hideFragment();
        switch (i) {
            case 0:
                //首页
                if (indexFragment == null) {
                    indexFragment = new IndexFragment();
                    ft.add(R.id.content, indexFragment);
                }
                ivMainTab01.setImageResource(R.mipmap.ic_main_one_selected);
                // tvHome.setTextColor(UIUtils.getColor(R.color.home_back_selected));
                tvMainTab01.setTextColor(getResources().getColor(R.color.colorMainBlue));
                viewIndexFirst.setVisibility(View.VISIBLE);
                viewIndexClassfical.setVisibility(View.INVISIBLE);
                viewIndexThird.setVisibility(View.INVISIBLE);
                viewIndexFourth.setVisibility(View.INVISIBLE);
                ft.show(indexFragment);
                break;
            case 1:

                //投资
                if (classFicationFragment == null) {
                    classFicationFragment = new ClassificationFragment();
                    ft.add(R.id.content, classFicationFragment);
                }
                ivMainTab05.setImageResource(R.mipmap.ic_main_five_selected);
                //  tvTouzi.setTextColor(UIUtils.getColor(R.color.home_back_selected));
                tvMainTab05.setTextColor(getResources().getColor(R.color.colorMainBlue));
                viewIndexFirst.setVisibility(View.INVISIBLE);
                viewIndexClassfical.setVisibility(View.VISIBLE);
                viewIndexThird.setVisibility(View.INVISIBLE);
                viewIndexFourth.setVisibility(View.INVISIBLE);
                ft.show(classFicationFragment);
                break;
            case 2:

                //资产
                if (shoppingFragment == null) {
                    shoppingFragment = new ShoppingFragment2();
                    ft.add(R.id.content, shoppingFragment);
                }
                ivMainTab02.setImageResource(R.mipmap.ic_main_three_selected);
                tvMainTab02.setTextColor(getResources().getColor(R.color.colorMainBlue));
                // tvMe.setTextColor(UIUtils.getColor(R.color.home_back_selected));
                viewIndexFirst.setVisibility(View.INVISIBLE);
                viewIndexClassfical.setVisibility(View.INVISIBLE);
                viewIndexThird.setVisibility(View.VISIBLE);
                viewIndexFourth.setVisibility(View.INVISIBLE);
                ft.show(shoppingFragment);
                break;
            case 3:

                //更多
                if (mineFragment == null) {
                    mineFragment = new MineFragment();
                    ft.add(R.id.content, mineFragment);
                }
                ivMainTab04.setImageResource(R.mipmap.ic_main_four_selected);
                // tvMore.setTextColor(UIUtils.getColor(R.color.home_back_selected));
                tvMainTab04.setTextColor(getResources().getColor(R.color.colorMainBlue));
                viewIndexFirst.setVisibility(View.INVISIBLE);
                viewIndexClassfical.setVisibility(View.INVISIBLE);
                viewIndexThird.setVisibility(View.INVISIBLE);
                viewIndexFourth.setVisibility(View.VISIBLE);
                ft.show(mineFragment);
                break;
        }
//        ft.commit();
//         commitAllowingStateLoss

        ft.commitAllowingStateLoss();
    }*/
/*

    private void reSetTab() {
        ivMainTab01.setImageResource(R.mipmap.ic_main_one_unselected);
        ivMainTab05.setImageResource(R.mipmap.ic_main_five_unselected);
        ivMainTab02.setImageResource(R.mipmap.ic_main_three_unselected);
        ivMainTab04.setImageResource(R.mipmap.ic_main_four_unselected);
        tvMainTab02.setTextColor(getResources().getColor(R.color.colorTextGrey));
        tvMainTab01.setTextColor(getResources().getColor(R.color.colorTextGrey));
        tvMainTab03.setTextColor(getResources().getColor(R.color.colorTextGrey));
        tvMainTab05.setTextColor(getResources().getColor(R.color.colorTextGrey));
        tvMainTab04.setTextColor(getResources().getColor(R.color.colorTextGrey));
        */
/*tvHome.setTextColor(UIUtils.getColor(R.color.home_back_unselected));
        tvTouzi.setTextColor(UIUtils.getColor(R.color.home_back_unselected));
        tvMe.setTextColor(UIUtils.getColor(R.color.home_back_unselected));
        tvMore.setTextColor(UIUtils.getColor(R.color.home_back_unselected));*//*

    }

*/

    private void hideFragment() {
        if (indexFragment != null) {
            ft.hide(indexFragment);
        }
        if (classFicationFragment != null) {
            ft.hide(classFicationFragment);
        }
        if (shoppingFragment != null) {
            ft.hide(shoppingFragment);
        }
        if (mineFragment != null) {
            ft.hide(mineFragment);
        }

    }


    /**
     * 连续两次推出键退出程序
     */
    private long atTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            Log.d("TIME", "当前时间:" + System.currentTimeMillis());

            if (System.currentTimeMillis() - atTime > 2000) {
                //  Toast.makeText(this, "再按一次退出应用程序", Toast.LENGTH_SHORT).show();
                LovelyToast.makeText(MainActivity.this, "再次点击确定退出", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
                //  Toast.makeText(context, "asdf", Toast.LENGTH_SHORT).show();
                //   toast.setGravity(Gravity.CENTER, 0, 0);
                //  toast.show();
                atTime = System.currentTimeMillis();
            } else {
                LovelyToast.cancel();
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 这里用到的了EventBus框架  博客教程:http://blog.csdn.net/lmj623565791/article/details/40920453
     *
     * @param type
     */
    public void onEventMainThread(Integer type) {
        Log.e("---", "EventBus收到int:" + type);
        if (type == 1) {
            llMainTab02.performClick();
//            setSelect(2);
            onClick(llMainTab02);
        }
    }

    private void activateView(View view, int mipmapId, int colorId) {
        try {
            ViewGroup viewGroup = (ViewGroup) view;
            ((ImageView) viewGroup.getChildAt(0)).setImageResource(mipmapId);
            ((TextView) viewGroup.getChildAt(1)).setTextColor(colorId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {

        activateView(llMainTab01, R.mipmap.ic_main_one_unselected, getResources().getColor(R.color.gray66));
        activateView(llMainTab05, R.mipmap.ic_main_five_unselected, getResources().getColor(R.color.gray66));
        activateView(llMainTab02, R.mipmap.ic_main_three_unselected, getResources().getColor(R.color.gray66));

        activateView(llMainTab04, R.mipmap.ic_main_four_unselected, getResources().getColor(R.color.gray66));
        switch (v.getId()) {
            case R.id.ll_main_tab_01:
                activateView(v, R.mipmap.ic_main_one_selected, getResources().getColor(R.color.colorMainBlue));
                navigationManager.show(IndexFragment.class);
//                    LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("refresh"));
                break;


            case R.id.ll_main_tab_05:

                activateView(v, R.mipmap.ic_main_five_selected, getResources().getColor(R.color.colorMainBlue));
                navigationManager.show(ClassificationFragment.class);
//                        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("refresh"));

                break;

            case R.id.ll_main_tab_02:

                activateView(v, R.mipmap.ic_main_three_selected, getResources().getColor(R.color.colorMainBlue));

                navigationManager.show(ShoppingFragment2.class);
                LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("refresh"));
                break;

            //更多
            /*    if (mineFragment == null) {
                    mineFragment = new MineFragment();
                    ft.add(R.id.content, mineFragment);
                }
                ivMainTab04.setImageResource(R.mipmap.ic_main_four_selected);
                // tvMore.setTextColor(UIUtils.getColor(R.color.home_back_selected));
                tvMainTab04.setTextColor(getResources().getColor(R.color.colorMainBlue));
                viewIndexFirst.setVisibility(View.INVISIBLE);
                viewIndexClassfical.setVisibility(View.INVISIBLE);
                viewIndexThird.setVisibility(View.INVISIBLE);
                viewIndexFourth.setVisibility(View.VISIBLE);
                ft.show(mineFragment);*/
            case R.id.ll_main_tab_04:

                activateView(v, R.mipmap.ic_main_four_selected, getResources().getColor(R.color.colorMainBlue));
                navigationManager.show(MineFragment.class);
//                        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("refreshMine"));


                break;

        }
    }

    @Override
    public void openDetails(boolean smooth) {

    }

    @Override
    public void closeDetails(boolean smooth) {

    }
}
